import requests
from bs4 import BeautifulSoup
def gethtml(url):
    try:
        re=requests.get(url,timeout=10)
        re.raise_for_status()
        re.encoding="utf-8"
        print("获取成功")
        return re.text
    except:
        print("获取失败")
        return ""
def gethtmldata(html):
    soup=BeautifulSoup(html,"html.parser")
    divs=soup.find_all('div',{"class":"right-1"})
    all_ls=[]
    for a in divs[0].find_all('a'):
        ls = []
        if(len(a)!=1):
            ls.append(a.find("em").string+a.find("i").string+"日")
            ls.append(a.find("h3").string)
            ls.append(a.attrs["href"])
        all_ls.append(ls)
        ls=[]
    return all_ls

def writedata(ls):
    with open(r"中国大学新闻.csv","w") as f:
        for line in ls:
            f.write(",".join(line)+"\n")

def main():
    url="http://www.zjitc.net/xwzx/xyxw.htm"
    html=gethtml(url)

    gethtmldata(html)
    writedata(gethtmldata(html))

if __name__ == '__main__':
    main()
